<?php

namespace App\Models;

use Carbon\Carbon;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Reserves
 *
 * @property int $id
 * @property int $interviewee_information_id 受访者id
 * @property string $reason 访问原因
 * @property mixed $device_groups 通行方式
 * @property int $source 访问区域
 * @property string $start_time 开始时间
 * @property string $end_time 结束时间
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property int $status 状态{0:关闭,1:开启}
 * @property-read \App\Models\IntervieweeInformation|null $intervieweeInformation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Visitor[] $visitor
 * @property-read int|null $visitor_count
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves newQuery()
 * @method static \Illuminate\Database\Query\Builder|Reserves onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves query()
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereDeviceGroups($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereEndTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereIntervieweeInformationId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereReason($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereSource($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereStartTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Reserves whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|Reserves withTrashed()
 * @method static \Illuminate\Database\Query\Builder|Reserves withoutTrashed()
 * @mixin \Eloquent
 * @property-read string|null $end_time_u_t_c
 * @property-read string|null $start_time_u_t_c
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\VisitorReservesLog[] $visitorReservesLog
 * @property-read int|null $visitor_reserves_log_count
 */
class Reserves extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;
    use HasFactory;

    static array $reason = [
        '面试'     => '面试',
        '商务洽谈' => '商务洽谈',
        '技术交流' => '技术交流',
        '其他'     => '其他',
    ];

    static array $deviceGroups = [
        12 => '刷脸',
        13 => '二维码',
    ];

    static array $source = [
        3 => '访客通道'
    ];

    protected $casts = [
        'device_groups' => 'array',
    ];

    /**
     * 访客
     * @return BelongsToMany
     */
    public function visitor(): BelongsToMany
    {
        return $this->belongsToMany(Visitor::class, 'visitor_reserves');
    }

    /**
     * 受访者
     * @return BelongsTo
     */
    public function intervieweeInformation(): BelongsTo
    {
        return $this->belongsTo(IntervieweeInformation::class);
    }

    public function getStartTimeUTCAttribute($value): ?string
    {
        return $this->start_time ? Carbon::parse($this->start_time)->setTimezone('UTC')->format('Y-m-d\TH:i:s.vp') : null;
    }

    public function getEndTimeUTCAttribute($value): ?string
    {
        return $this->end_time ? Carbon::parse($this->end_time)->setTimezone('UTC')->format('Y-m-d\TH:i:s.vp') : null;
    }

    /**
     * 访客预约日志
     * @return HasMany
     */
    public function visitorReservesLog(): HasMany
    {
        return $this->hasMany(VisitorReservesLog::class);
    }

}
